
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>订单管理</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <li class="layui-this">转账订单</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
          <div class="layui-row">
            <div class="layui-form" style="float:right; margin-bottom: 10px;">
              <div class="layui-form-item" style="margin:0;">
                <div class="layui-input-inline">
                  <input type="text" name="createTimeStart" id="createTimeStart" autocomplete="off" placeholder="开始时间" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="createTimeEnd" id="createTimeEnd" autocomplete="off" placeholder="结束时间" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="mchId" id="mchId" placeholder="商户ID" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="transOrderId" id="transOrderId" placeholder="转账订单号" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="mchTransNo" id="mchTransNo" placeholder="商户订单号" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <select name="type" id="status" lay-search="">
                    <option value="-99">转账状态</option>
                    <option value="0">订单生成</option>
                    <option value="1">转账中</option>
                    <option value="2">转账成功</option>
                    <option value="3">转账失败</option>
                    <option value="4">处理完成</option>
                  </select>
                </div>
                <button id="search" class="layui-btn" data-type="reload">搜索</button>
              </div>
            </div>
            <div class="layui-form-item">
              <blockquote class="layui-elem-quote" id="amountTip">
                提交订单数:<sapn id="allTotalCount" style="color: blue; margin-right: 10px;"></sapn>
                订单总金额:<span id="allTotalAmount" style="color: blue; margin-right: 10px;"></span>
                成功订单数:<span id="successTotalCount" style="color: green; margin-right: 10px;"></span>
                成功总金额:<span id="successTotalAmount" style="color: green; margin-right: 10px;"></span>
                渠道总成本:<span id="successTotalChannelCost" style="color: green; margin-right: 10px;"></span>
              </blockquote>
            </div>
          </div>
          <table id="JeePay_Mgr_TransOrder_dataAll" lay-filter="JeePay_Mgr_TransOrder_dataAll"></table>
        </div>

      </div>
    </div>
  </div>
</div>

<script type="text/html" id="jeepayBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
</script>

<script>
layui.use(['admin', 'table', 'util', 'laydate'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,table = layui.table
  ,element = layui.element
  ,laydate = layui.laydate
  ,form = layui.form;
  
  element.render('breadcrumb', 'breadcrumb');

  $.setCountResult = function(mchId, transOrderId, mchTransNo, createTimeStart, createTimeEnd) {
    admin.req({
      type: 'get',
      url: layui.setter.baseUrl + '/trans_order/count',
      data: {
        mchId : mchId,
        transOrderId : transOrderId,
        mchTransNo : mchTransNo,
        createTimeStart : createTimeStart,
        createTimeEnd : createTimeEnd
      },
      error: function(err){
        layer.alert(JSON.stringify(err.field), {
          title: '错误提示'
        })
      },
      success: function(res){
        if(res.code == 0){
          $('#allTotalCount').html(res.data.allTotalCount);
          $('#allTotalAmount').html("￥" + res.data.allTotalAmount/100.00);
          $('#successTotalCount').html(res.data.successTotalCount);
          $('#successTotalAmount').html("￥" + res.data.successTotalAmount/100.00);
          $('#successTotalChannelCost').html("￥" + res.data.successTotalChannelCost/100.00);
        }
      }
    });
  };

  var tplStatus = function(d){
    if(d.status == 0) {
      return "<span style='color: blue'>订单生成</span>";
    }else if(d.status == 1) {
      return "<span style='color: orangered'>转账中</span>";
    }else if(d.status == 2) {
      return "<span style='color: green'>转账成功</span>";
    }else if(d.status == 3) {
      return "<span style='color: red'>转账失败</span>";
    }else if(d.status == 4) {
      return "<span style='color: darkgreen'>处理完成</span>";
    }
  };
// 转账结果:0-不确认结果,1-等待手动处理,2-确认成功,3-确认失败
  var tplResult = function(d){
    if(d.result == 0) {
      return "<span style='color: blue'>不确认结果</span>";
    }else if(d.result == 1) {
      return "<span style='color: orangered'>等待处理</span>";
    }else if(d.result == 2) {
      return "<span style='color: green'>确认成功</span>";
    }else if(d.result == 3) {
      return "<span style='color: red'>确认失败</span>";
    }
  };

  // 初始化统计结果
  $.setCountResult();

  //用户列表
  table.render({
    elem: '#JeePay_Mgr_TransOrder_dataAll'
    ,url: layui.setter.baseUrl + '/trans_order/list' //用户列表接口
    ,where: {
        access_token: layui.data(layui.setter.tableName).access_token
    }
    ,id: 'tableReload'
    ,page: true
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'transOrderId', width: 220, title: '转账单号'}
      ,{field: 'mchTransNo', width: 220, title: '商户单号'}
      ,{field: 'mchId', title: '商户ID'}
      ,{field: 'amount', title: '转账金额', templet: '<div>{{ d.amount/100 }}</div>'}
      ,{field: 'status', title: '转账状态', templet: tplStatus}
      ,{field: 'result', title: '转账结果', templet: tplResult}
      ,{field: 'createTime', title: '创建时间', templet: '<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") }}</div>'}
      ,{field: 'edit', title: '操作', toolbar: '#jeepayBar' }
    ]]
    ,skin: 'line'
  });

  //监听工具条
  table.on('tool(JeePay_Mgr_TransOrder_dataAll)', function(obj){
    var data = obj.data;
    if(obj.event === 'detail'){
      location.href = layui.setter.baseLocal + "order/trans/view.html?" + data.transOrderId;
    }
  });

  // 搜索
  var $ = layui.$, active = {
    reload: function(){
      var mchId = $('#mchId').val();
      var createTimeStart = $('#createTimeStart').val();
      var createTimeEnd = $('#createTimeEnd').val();
      var transOrderId = $('#transOrderId').val();
      var mchTransNo = $("#mchTransNo").val();
      var status = $("#status").val();

      $.setCountResult(mchId, transOrderId, mchTransNo, createTimeStart, createTimeEnd);

      //执行重载
      table.reload('tableReload', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
          mchId: mchId,
          transOrderId: transOrderId,
          mchTransNo: mchTransNo,
          status : status,
          createTimeStart : createTimeStart,
          createTimeEnd : createTimeEnd
        }
      });
    }
  };
  $('#search').on('click', function() {
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });

  laydate.render({
    elem: '#createTimeStart'
    ,type: 'datetime'
    ,format: 'yyyy-MM-dd HH:mm:ss'
  });

  laydate.render({
    elem: '#createTimeEnd'
    ,type: 'datetime'
    ,format: 'yyyy-MM-dd HH:mm:ss'
  });
  // 渲染表单
  form.render();
});
</script>